package mobi.hami.huapin.dao;

import mobi.hami.huapin.daobase.CrudMapper;
import mobi.hami.huapin.model.UserPresentSku;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Create by 小峰 on 2018/9/26.
 */
@Repository
public interface UserPresentSkuMapper  extends CrudMapper<UserPresentSku> {

    /**
     * 根据用户Id获取赠送的List
     * @param userId
     * @return
     */
    @Select("select  sku_id , count(0) mycount from user_present_sku where user_id=#{userId}\n" +
            "group by sku_id order by mycount desc")
    List<UserPresentSku> getUserPresentCount(Integer userId);

    /**
     * 查询该用户在该拼团下是否已经送过赠品
     * @param huapinId
     * @param userId
     * @return
     */
    @Select("select count(0) from user_present_sku where huapin_id=#{huapinId} and user_id=#{userId}")
    Integer getCountByUserAndHuapin(@Param("huapinId")Integer huapinId,@Param("userId")Integer userId);

    /**
     * 增加一条赠送商品的记录
     * @param userPresentSku
     * @return
     */
    @Insert("Insert into user_present_sku(user_id,huapin_id, order_id, sku_id) VALUES " +
            "(#{userPresentSku.user_id},#{userPresentSku.huapin_id},#{userPresentSku.order_id},#{userPresentSku.sku_id})")
    Integer saveUserPresentSkuByModel(@Param("userPresentSku")UserPresentSku userPresentSku);
}
